<template>
  <div class="app-container">
    <el-row>
      <el-form
        :model="queryParams"
        label-width="90px"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
      >
        <el-col :span="6">
          <el-form-item label="毕业年份">
            <el-select
              v-model="queryParams.dgsId"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in yearList"
                :key="item.DGS_ID"
                :label="item.GRAD_YEAR"
                :value="item.DGS_ID"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="学位">
            <el-select
              v-model="queryParams.edLevel"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="dict in dict.type.basic_speciality_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="毕业生状态">
            <el-select
              v-model="queryParams.gdpStatus"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="dict in dict.type.dep_grad_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="院系">
            <el-select
              v-model="queryParams.deptId"
              clearable
              placeholder="请选择"
              @change="handSpeciality"
            >
              <el-option
                v-for="dict in classTreeList"
                :key="dict.id"
                :label="dict.label"
                :value="dict.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="专业">
            <el-select
              v-model="queryParams.specialityId"
              clearable
              placeholder="请选择"
              @change="changeGrade"
            >
              <el-option
                v-for="dict in specialityList"
                :key="dict.VALUE"
                :label="dict.TITILE"
                :value="dict.VALUE"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="gradeId" label="年级">
            <el-select
              v-model="queryParams.gradeId"
              placeholder="请选择"
              @change="changeGrade"
              clearable
              class="elSelect"
            >
              <el-option
                v-for="item in gradeDate"
                :key="item.VALUE"
                :label="item.TITILE"
                :value="item.VALUE"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="班级">
            <el-select
              v-model="queryParams.classId"
              clearable
              placeholder="请选择"
            >
            <el-option
                v-for="dict in classListType"
                :key="dict.VALUE"
                :label="dict.TITLE"
                :value="dict.VALUE"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="学生注册号">
            <el-input
              v-model="queryParams.regCode"
              placeholder="请输入注册号"
              clearable
            />
          </el-form-item>
        </el-col>

        <el-col :span="6" style="float: right; text-align: right">
          <el-form-item>
            <div>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="onSearch"
                size="mini"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
                >重置</el-button
              >
            </div>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div style="float: right">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['departure:financial:export']"
            >导出数据</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="handleAdd"
            v-hasPermi="['departure:gradStuRoster:add']"
            >新增离校学生</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" :data="gradStuRosterList">
      <el-table-column label="学院专业班级" align="center" show-overflow-tooltip prop="dsc">
      </el-table-column>
      <el-table-column label="注册号" align="center" prop="regCode">
      </el-table-column>
      <el-table-column label="学生姓名" align="center" prop="studentName">
      </el-table-column>
      <el-table-column label="学生状态" align="center" prop="gradScope">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dep_grad_scope"
            :value="scope.row.gradScope"
          />
        </template>
      </el-table-column>
      <el-table-column label="学位" align="center" prop="edLevel">
      </el-table-column>
      <el-table-column label="毕业生状态" align="center" prop="gdpStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dep_grad_status"
            :value="scope.row.gdpStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="毕业离校确认时间"
        align="center"
        prop="gdpTime"
        width="180"
      >
        <template slot-scope="scope">
          {{ scope.row.gdpTime == null ? "暂无数据" : scope.row.gdpTime }}
        </template>
      </el-table-column>
      <el-table-column label="结算金额" align="center" prop="amount">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
          v-hasPermi="['departure:gradStuRoster:query']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleCancel(scope.row)"
            v-hasPermi="['departure:gradStuRoster:remove']"
            >取消离校</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改毕业离校学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="注册号" prop="dgsId">
          <el-input v-model.trim="regCode" placeholder="请输入学生注册号" />
        </el-form-item>
        <el-row>
          <div v-if="form.dsc">
            <el-col :span="18"
              ><p>{{ form.dsc }}</p></el-col
            >
            <el-col :span="6"
              ><p>{{ form.studentName }}</p></el-col
            >
            <el-col :span="6"
              ><p>入学年份:{{ form.gradeName }}年</p></el-col
            >
            <el-col :span="4"
              ><p>{{ form.asys }}年制</p></el-col
            >
            <el-col :span="4"
              ><p>{{ form.education }}</p></el-col
            >
            <el-col :span="8"
              ><p>学生状态：{{studentStatusData.toString()}}</p></el-col
            >
          </div>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="addLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGradStuRoster,
  addGradStuRoster,
  updateGradStuRoster,
  delGradStuRoster,
} from "@/api/departure/gradStuRoster";
import { getGradYearSelector } from "@/api/departure/gradsettings";
import { getDicts } from "@/api/system/dict/type";
import { getStudentDpm } from "@/api/psychological/basic";
import { getSpecialityByDeptId } from "@/api/information/unitMajor";
import { classTree, studentGrad } from "@/api/information/studentInfor";
import { getGradeSelect } from "@/api/information/gradeInfor";
import { getClassSelect } from "@/api/information/classInfor";

export default {
  name: "GradStuRoster",
  dicts: ["dep_grad_status", "dep_grad_scope", "basic_speciality_type"],

  data() {
    return {
      regCode: "",
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      addLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 毕业离校学生表格数据
      gradStuRosterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dgsId: null,
        specialityId: undefined,
        gdpStatus: null,
        classId: undefined
      },
      // 表单参数
      form: {},
      yearList: [],
      classListType: [],
      classTreeList: [],
      specialityList: [],
      gradeDate: [],
      studentStatusData: '',
      // 表单校验
      rules: {},
    };
  },
  watch: {
    regCode(curVal, oldVal) {
      // 实现input连续输入，只发一次请求
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        this.handleChangeName(curVal);
      }, 500);
    },
  },
  created() {
    getGradYearSelector().then((res) => {
      this.yearList = res.data;
      this.queryParams.dgsId = res.data[0].DGS_ID;
      this.getList();
    });

    classTree().then((response) => {
      this.classTreeList = response.data;
    });
  },
  methods: {
    // 获取搜索表单提交的数据
    onSearch() {
      // 查询条件和分页数据合并
      this.getList();
    },
    // 重置
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
         dgsId: null,
        specialityId: undefined,
        gdpStatus: null,
        classId: undefined
      };
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.queryParams.dgsId = this.yearList[0].DGS_ID;
      this.classListType = []
      this.gradeDate = []
      this.specialityList = []
      this.onSearch();
    },
    getClassList() {
      getClassSelect({
        gradeId: this.queryParams.gradeId,
        specialtyId: this.queryParams.specialityId,
      }).then((res) => {
        console.log(res, "班级");
        this.classListType = res.data;
      });
    },
    // 年级
    getGradeInfor() {
      getGradeSelect().then((response) => {
        this.gradeDate = response.data;
      });
    },
    changeGrade(value) {
      console.log(value, "ceeee");
      if (value != undefined) {
        if (this.queryParams.specialityId && this.queryParams.gradeId) {
          this.getClassList();
          this.classListType = [];
          this.queryParams.classId = "";
        } else {
          if (!value) {
            this.classListType = [];
            this.queryParams.classId = "";
          }
        }
      }
    },
    async handSpeciality() {
      this.queryParams.specialityId = ''
      this.queryParams.classId = ''
      this.classListType = [];
      this.getSpeciality() 
      this.getGradeInfor()
    },
    // 专业
    async getSpeciality() {
      if (this.queryParams.deptId) {
        await getSpecialityByDeptId({ deptId: this.queryParams.deptId }).then(res => {
          if (res.code == "200") {
            this.specialityList = res.data;
          } else {
            this.specialityList = [];
          }
        });
      } else {
        this.specialityList = [];
      }
    },
    handleChangeName(value) {
      console.log(this.form, value, "注册号");
      if (value === "") {
        return false;
      } else {
        studentGrad(value.toString()).then((response) => {
          this.form = response.data;
          getDicts("dep_grad_scope").then((res) => {
            let data = this.form.studentStatus.split(",");
            this.studentStatusData = res.data.filter(item => data.includes(item.dictValue)).map(item => item.dictLabel);
           
          });
        });
      }
    },
    /** 查询毕业离校学生列表 */
    getList() {
      this.loading = true;
      listGradStuRoster(this.queryParams).then((response) => {
        this.gradStuRosterList = response.rows;
        this.gradStuRosterList.forEach((item) => {
          item.gradScope = item.gradScope.split(",");
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 新增离校学生操作按钮
    handleAdd() {
      this.open = true;
      this.regCode = "";
      this.form = {};
      this.title = "新增";
    },
    cancel() {
      this.regCode = "";
      this.form = {};
      this.open = false;
    },
    /** 详情 */
    handleUpdate(row) {
      this.$router.push({
        path: "/departure/gradStuRoster/departureDetails",
        query: { dgsrId: row.dgsrId },
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.regCode) {
        if (this.addLoading) return;

        this.addLoading = true;
        try {
          this.form.dgsId = this.queryParams.dgsId;
          this.$refs["form"].validate((valid) => {
            if (valid) {
              addGradStuRoster(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.addLoading = false;
                this.open = false;
                this.regCode = "";
                this.form = {};
                this.getList();
              });
            }
          });
          // 处理成功逻辑
        } catch (error) {
          // 处理错误
        } finally {
          this.addLoading = false;
        }
      } else {
        this.$modal.msgError("请填写注册号");
      }
      
    },
    /** 取消按钮 */
    handleCancel(row) {
      const dgsrIds = row.dgsrId;
      this.$modal
        .confirm(
          "取消后，该学生 " +
            row.studentName +
            '将从离校名单中删除，无法进行毕业离校，请确认是否取消"'
        )
        .then(function () {
          return delGradStuRoster(dgsrIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("取消离校成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/departure/financial/export",
        {
          ...this.queryParams,
        },
        `毕业离校学生${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
